<?php

/**
 * класс для обработки изменения пользовательских данных
 *
 * @author kingop
 */
class FormProcessor_EditProfile extends FormProcessor {

    protected $db = null;
    public $user = null;

    public function __construct($db, $user_id) {
        parent::__construct();

        $this->db = $db;
        $this->user = new DatabaseObject_User($db);
        $this->user->load($user_id);
        $this->id = $user_id;
        $this->login = $this->user->login;
        $this->email = $this->user->email;
        $this->country_id = $this->user->country_id;
        $this->region_id = $this->user->region_id;
        $this->city_id = $this->user->city_id;
        $this->first_name = $this->user->first_name;
        //$this->last_name = $this->user->last_name;
        $this->phone = $this->user->phone;
        $this->gender = $this->user->gender;
        $this->photo = $this->user->photo;
        $this->content = $this->user->content;
        $this->custom_phones = $this->user->custom_phones;
       
       
    }

    public function process(Zend_Controller_Request_Abstract $request) {
        // проверим логин
        $this->login = trim($request->getPost('login'));
        if (strlen($this->login) == 0)
            $this->addError('login', 'Вы должны указать логин');
        else if (!DatabaseObject_User::IsValidUsername($this->login))
            $this->addError('login', 'Логин содержит недопустимые символы');
        else if ($this->user->usernameExists($this->login) && $this->user->login != $this->login)
            $this->addError('login', 'Выбранный вами логин уже занят');
        else
            $this->user->login = $this->login;

        // проверим, был ли введен новый пароль
        $this->password = $this->sanitize($request->getPost('password'));
        $this->password_again = $this->sanitize($request->getPost('password_again'));

        if (strlen($this->password) > 0 || strlen($this->password_again) > 0) {
            if (strlen($this->password) == 0)
                $this->addError('password', 'Пожалуйста, укажите новый пароль');
            else if (strlen($this->password_again) == 0)
                $this->addError('password_again', 'Пожалуйста, подтвердите свой пароль');
            else if ($this->password != $this->password_again)
                $this->addError('password_again', 'Введенные пароли не совпадают');
            else
                $this->user->password = $this->password;
        }
        
          // проверим Имя
        $this->first_name = trim($request->getPost('first_name'));
       // if (strlen($this->first_name) == 0)
           // $this->addError('first_name', 'Вы должны указать имя');
       // else
            $this->user->first_name = $this->first_name;

        /*$this->last_name = trim($request->getPost('last_name'));
        if (strlen($this->last_name) == 0 && strlen($this->first_name) == 0)
            $this->addError('last_name', 'Вы должны указать Имя или название магазина');
        else
            $this->user->last_name = $this->last_name;*/
        
        $this->phone = trim($request->getPost('phone'));
        if (strlen($this->phone) == 0)
            $this->addError('phone', 'Вы должны указать основной телефон телефон');
        else
            $this->user->phone = $this->phone;
        
        // проверим область
        $this->country_id = trim($request->getPost('country_id'));
        if (strlen($this->country_id) == 0)
            $this->addError('country_id', 'Вы должны указать страну ');
        else
            $this->user->country_id = $this->country_id;
        
          // проверим город
        $this->region_id = trim($request->getPost('region_id'));
        if (strlen($this->region_id) == 0)
            $this->addError('region_id', 'Вы должны указать регион');
        else
            $this->user->region_id = $this->region_id;
        
         // проверим город
        $this->city_id = trim($request->getPost('city_id'));
        if (strlen($this->city_id) == 0)
            $this->addError('city_id', 'Вы должны указать город');
        else
            $this->user->city_id = $this->city_id;
        
           // проверим город
        $this->content = trim($request->getPost('content'));
        $this->user->content = $this->content;
        
        $this->custom_phones = trim($request->getPost('custom_phones'));
        $this->user->custom_phones = $this->custom_phones;

       // проверим email
        $this->email = trim($request->getPost('email'));
        if (strlen($this->email) == 0)
            $this->addError('email', 'Вы должны указать email');
        else if($this->user->emailExists($this->email) && $this->user->email != $this->email) {
            $this->addError('email', 'Указанный вами email уже занят');
        }
        else
            $this->user->email = $this->email;
         $this->file = $request->getPost('filesrc');
         if ($this->file)
           foreach ($this->file as $val) {
                 $this->user->photo ='/uploads/'.$val;
                }
       

        // если не было ошибок, сохраним пользователя
        if (!$this->_validateOnly && !$this->hasError()) {
            $this->user->save();
            
        }
        
        return !$this->hasError();
    }

}

?>